import axios from 'axios'
import store from '@/store/index.js'
import jsonBig from 'json-bigint'
const request = axios.create({
  timeout: 500,
  baseURL: ' http://toutiao.itheima.net/', // 基础路径
  transformResponse: [function (data) {
    try {
      // 如果转换成功则返回转换的数据结果
      return jsonBig.parse(data)
    } catch (err) {
      // 如果转换失败，则包装为统一数据格式并返回
      return {
        data
      }
    }
  }]
})

export default request

request.interceptors.request.use(function (config) {
  const user = store.state.user
  if (user && user.token) {
    config.headers = { Authorization: `Bearer ${store.state.user.token}` }
  }
  return config
})
